package com.xcl.controller;

import com.xcl.pojo.Article;
import com.xcl.pojo.Result;
import com.xcl.service.ArticleService;
import jakarta.validation.Valid;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    //查询文章列表
    @RequestMapping("/findArticles")
    public Result findArticles(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String state){
        List<Article> lst = articleService.findArticle(categoryId,state);
        return Result.success(lst);
    }

    @RequestMapping("/add")
    public Result addArticle(@Valid @RequestBody Article article){
        Integer i = articleService.addArticle(article);
        if(i > 0){
            return Result.success();
        }
        return Result.error("添加失败");
    }

    @RequestMapping("/update")
    public Result updateArticle(@Valid @RequestBody Article article){
        Integer i = articleService.updateArticle(article);
        if(i > 0){
            return Result.success();
        }
        return Result.error("修改失败");
    }
}
